Update Operation

For the Update operation, you need to implement a route that allows users to update existing transactions. You'll again handle both GET and POST HTTP requests - GET for displaying the current transaction data in a form, and POST for processing the updated data sent by the user.

Complete the following steps to implement the Update operation.

  1. Create a function named edit_transaction that handles both GET and POST requests. This function should accept a parameter, transaction_id.

  2. Decorate the function with @app.route and use the route string /edit/<int:transaction_id>. The <int:transaction_id> part in the URL is a placeholder for any integer. Flask will pass this integer to your function as the transaction_id argument.

  3. If the request method is GET, find the transaction with the ID that matches transaction_id and use render_template to display a form pre-populated with the current data of the transaction using a template named edit.html.

  4. If the request method is POST, use request.form to get the updated data, find the transaction with the ID that matches transaction_id and modify its data, then redirect the user back to the list of transactions.

Click here for solution
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  1. # Update operation: Display edit transaction form
  2. # Route to handle the editing of an existing transaction
  3. @app.route("/edit/<int:transaction_id>", methods=["GET", "POST"])
  4. def edit_transaction(transaction_id):
  5. # Check if the request method is POST (form submission)
  6. if request.method == 'POST':
  7. # Extract the updated values from the form fields
  8. date = request.form['date'] # Get the 'date' field value from the form
  9. amount = float(request.form['amount'])# Get the 'amount' field value from the form and convert it to a float
  10. # Find the transaction with the matching ID and update its values
  11. for transaction in transactions:
  12. if transaction['id'] == transaction_id:
  13. transaction['date'] = date # Update the 'date' field of the transaction
  14. transaction['amount'] = amount # Update the 'amount' field of the transaction
  15. break # Exit the loop once the transaction is found and updated
  16. # Redirect to the transactions list page after updating the transaction
  17. return redirect(url_for("get_transactions"))
  18. # If the request method is GET, find the transaction with the matching ID and render the edit form
  19. for transaction in transactions:
  20. if transaction['id'] == transaction_id:
  21. # Render the edit form template and pass the transaction to be edited
  22. return render_template("edit.html", transaction=transaction)
  23. # If the transaction with the specified ID is not found, handle this case (optional)
  24. return {"message": "Transaction not found"}, 404

Now, the code will look like this:
Correct code reference

Note: There may be multiple ways of achieving the same result. Please use the solution given above only as a reference.